<?php defined('SYSPATH') OR die('No direct access allowed.');

class Forum_Moderated_Category_Model extends ORM {
	
	//protected $db = 'forum';
	protected $belongs_to = array('user'=>'forum_user', 'category'=>'forum_category');

	public function get_categories($user_id = NULL) {
		if (is_null($user_id)) return array();
		$categories = $this->db->select('category_id')->where('user_id', $user_id)->get($this->table_name);
		$result = array();
		foreach($categories as $category) 
			$result[] = $category->category_id;
		return $result;
	}

	public function get_moderators($category = NULL, $object = FALSE) {
		if (is_null($category)) {
			$users = $this->db
								->select('user_id')
								->where('category_id', NULL)
								->get($this->table_name);
		}
		else {
			if (! $category instanceof Forum_Category_Model)
				$category = ORM::factory('forum_category', $category);
			if (!$category->loaded) return array();
			// get all parent categories id
			$parents = $category->get_parents(TRUE)->primary_key_array();
			$users = $this->db
							->select('user_id')
							->in('category_id', $parents)
							->get($this->table_name);
		}
		$result = array();
		foreach($users as $user) 
			$result[] = ($object === TRUE) ? ORM::factory('forum_user', $user->user_id) : $user->user_id;
		return $result;		
	}
}